package com.coocaa.ops.admin.v1.poster.biz.impl;


import com.coocaa.ops.admin.v1.poster.biz.IGlobalPictureBiz;
import com.coocaa.ops.admin.v1.poster.constant.PosterTypeConstants;
import com.coocaa.ops.admin.v1.poster.mapper.IGlobalPictureMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import com.coocaa.ops.common.tool.algorithm.CrcUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bjh
 * @description 素材图片表-biz层接口实现
 * @date 2020-06-13 10:50:35
 */
@Service
public class GlobalPictureBizImpl extends BaseBizImpl<IGlobalPictureMapper, GlobalPictureEntity> implements IGlobalPictureBiz {

    @Override
    public GlobalPictureEntity queryOrSavePoster(String pictureUrl, Integer userId) {
        Assert.hasText(pictureUrl, "海报Url不能为空!");
        QueryTemplate<GlobalPictureEntity> query = QueryTemplate.getEqQueryTemplate("flag", 1);
        query.lambda().eq(GlobalPictureEntity::getPictureUrl, pictureUrl)
                .eq(GlobalPictureEntity::getUrlCrc, CrcUtil.getCrc32(pictureUrl));
        ;
        int count = getMapper().selectCount(query);
        if (count > 0) {
            List<GlobalPictureEntity> pictureEntityList = getMapper().selectList(query);
            return pictureEntityList.get(0);
        }
        GlobalPictureEntity pictureEntity = new GlobalPictureEntity();
        pictureEntity.setStatus(ResourceStatusEnums.ON_LINE.getStatus());
        pictureEntity.setPictureCategory(-1);
        pictureEntity.setPictureUrl(pictureUrl);
        pictureEntity.setCreator(userId);
        pictureEntity.setModifier(userId);
        pictureEntity.setPictureType(PosterTypeConstants.FRONT);
        pictureEntity.setUrlCrc(CrcUtil.getCrc32(pictureEntity.getPictureUrl()));
        pictureEntity = this.add(pictureEntity);
        return pictureEntity;
    }

    @Override
    public boolean isPosterHasUsed(Integer posterId) {
        Integer count = getMapper().checkPosterHasUsed(posterId);
        return null != count && count > 0;
    }
}
